<?php

namespace app\data\controller\control;
use app\data\model\ControlClaimListModel;
use app\data\model\StorehouseDataModel;
use app\data\model\StorehouseModel;
use app\data\model\EnterStorehouseItemModel;
use think\admin\Controller;

/**
 * 仓库数据管理
 * Class Goods
 * @package app\data\controller\shop
 */
class StorehouseData extends Controller
{
    /**
     * 绑定数据表
     * @var string
     */
    private $table = 'SnEnterStorehouseItem';

    /**
     * 最大分类等级
     * @var integer
     */
    protected $cateLevel;

    /**
     * 仓库数据管理
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {

        // $StirehouseData = new StorehouseDataModel();
        // //获取剩余物资
        // $newsData = $StirehouseData->whereIn('material_code','0928019111354')->column('sum(material_num)','material_code');
        // dump($newsData);die;
        $this->title = '仓库数据管理';
        $query = $this->_query($this->table);
        if(input('material_code')){
            $where[] = ['material_code','like',input('material_code').'%'];
            $query->where($where);
        }
        
        $this->storehouse_list = StorehouseModel::where('deleted',0)->select()->toArray();
        // 列表排序并显示
        $query->like('material_name,material_model')->group('material_code')->field('*,sum(material_num) as material_num');
        $query->equal('status,storehouse_id')->order('material_code asc')->page();
    }

    /**
     * 数据列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _page_filter(array &$data)
    {
        $newData = array();
        foreach ($data as $k=>$v){
            $newData[$v['material_code']] = $v;
        }
        $ControlClaimListModel = new ControlClaimListModel();
        //获取出库数据
        if(input('enter_time')){
            $enter_time = explode(' - ',input('enter_time'));
            $claim_nums = $ControlClaimListModel->whereIn('status',[0,1])->whereIn('material_code',array_keys($newData))->whereBetween('plan_time',$enter_time)->group('material_code')->column('sum(material_num)','material_code');
        }else{
            $claim_nums = $ControlClaimListModel->whereIn('status',[0,1])->whereIn('material_code',array_keys($newData))->group('material_code')->column('sum(material_num)','material_code');
        }
        foreach ($newData as $ks=>$vs){
            $newData[$ks]['material_xjprice'] = sprintf("%01.2f", $vs['material_price'] * $vs['material_num']);
            if(!isset($claim_nums[$ks])){
                $newData[$ks]['claim_num'] = 0;
                $newData[$ks]['claim_xjprice'] = 0;
                $newData[$ks]['surplus_num'] = $newData[$ks]['material_num'];
                $newData[$ks]['surplus_xjprice'] = sprintf("%01.2f", $newData[$ks]['material_price'] * $newData[$ks]['material_num']);
            }else{
                $newData[$ks]['claim_num'] = $claim_nums[$ks];
                $newData[$ks]['claim_xjprice'] = sprintf("%01.2f", $newData[$ks]['material_price'] * $claim_nums[$ks]);
                $newData[$ks]['surplus_num'] = sprintf("%01.2f", $newData[$ks]['material_num'] - $claim_nums[$ks]);
                $newData[$ks]['surplus_xjprice'] = sprintf("%01.2f", $newData[$ks]['material_price'] * sprintf("%01.2f", $newData[$ks]['material_num'] - $claim_nums[$ks]));
                
                        
            }
        }
        //获取期初剩余数据
        $EnterStorehouseItemModel = new EnterStorehouseItemModel();
        if(input('enter_time')){
            $enter_time = explode(' - ',input('enter_time'));
            $start_nums = $EnterStorehouseItemModel->where('status',1)->whereIn('material_code',array_keys($newData))->where('enter_time','<',$enter_time[0])->column('material_num','material_code');
        }else{
            // $start_nums = $EnterStorehouseItemModel->where('status',1)->whereIn('material_code',array_keys($newData))->where('enter_time','<',date('Y-m-d H:i:s',time()))->column('material_num','material_code');
            $start_nums = array();
        }
        foreach ($newData as $ks=>$vs){
            if(isset($start_nums[$ks])){
                $newData[$ks]['start_num'] = $start_nums[$ks];
                $newData[$ks]['start_xjprice'] = sprintf("%01.2f", $newData[$ks]['material_price'] * $start_nums[$ks]);
                $newData[$ks]['surplus_num'] = sprintf("%01.2f", $newData[$ks]['surplus_num'] + $start_nums[$ks]);
                $newData[$ks]['surplus_xjprice'] = sprintf("%01.2f", $newData[$ks]['material_price'] * sprintf("%01.2f", $newData[$ks]['surplus_num'] + $start_nums[$ks]));
            }
            
        }
        sort($newData);
        $data = $newData;
    }

    /**
     * 删除仓库数据
     * @auth true
     * @throws \think\db\exception\DbException
     */
    public function remove()
    {
        $this->_save($this->table, $this->_vali([
            'deleted.in:0,1'  => '状态值范围异常！',
            'deleted.require' => '状态值不能为空！',
        ]), 'id');
    }

}